//index.js
var app = getApp()
var Keys = require("../../assets/data/keys.js")

Page({
	data: {
		goodsList: {
			saveHidden: true,
			totalPrice: 0,
			allSelect: true,
			noSelect: false,
			list: [],
			pic: false,
		},
	},
	onLoad: function () {
		// this.onShow();
	},
	onShow: function () {
		var _this = this;
		var shopList = [];

		var shopCarInfoMem = wx.getStorageSync('shopCarInfo');
		wx.request({
			url: "https://www.chnode.xyz/api/Car.ashx?",
			data: {
				DataType: 'CarList',
				Token: getApp().data.Token,
        AS_ID: getApp().data.AS_ID,
        A_ID: getApp().data.A_ID,
			},

			success: function (res) {
				//if (res.data.isok == "true") {
				var shopCarInfoMem = res.data;
				if (shopCarInfoMem && shopCarInfoMem.datas) {
					shopList = shopCarInfoMem.datas
				}
				_this.data.goodsList.list = shopList;
				_this.setGoodsList(_this.getSaveHide(), _this.totalPrice(), _this.allSelect(), _this.noSelect(), shopList);
				//}
			},
		});
	},
	onHide: function () {
		var _this = this;

		var list_all = _this.data.goodsList.list;
		var firstcount = "";
		var delcs_id = "";
		var csid = "";
		for (let i = 0; i < list_all.length; i++) {
			csid += list_all[i].C_ID + ",";
			firstcount += list_all[i].C_Firstcount + ",";
		}


		if (list_all.length > 0) {
			csid = csid.substring(0, csid.length - 1);
			firstcount = firstcount.substring(0, firstcount.length - 1);
		}
		wx.request({
			url: "https://www.chnode.xyz/api/Car.ashx?",
			data: {
				DataType: 'CarUpdate',
				C_ID: csid,
				Number: firstcount,
				Token: getApp().data.Token
			},
			success: function (res) {
				if (res.data.isok == "true") {
				}
			},
		});
	},
	toIndexPage: function () {
		wx.switchTab({
			url: "/pages/index/index"
		});
	},

	delItem: function (e) {
		var index = e.currentTarget.dataset.index;
		var list = this.data.goodsList.list;
		list.splice(index, 1);
		this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
	},


	selectTap: function (e) {
		var index = e.currentTarget.dataset.index;
		var list = this.data.goodsList.list;
		if (index !== "" && index != null) {
			list[parseInt(index)].active = !list[parseInt(index)].active;
			this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
		}
	},
	totalPrice: function () {
		var list = this.data.goodsList.list;
		var total = 0;
		for (var i = 0; i < list.length; i++) {
			var curItem = list[i];
			if (curItem.active) {
				total += parseFloat(curItem.P_Price) * curItem.C_Firstcount;
			}
		}
		total = parseFloat(total.toFixed(2));//js浮点计算bug，取两位小数精度
		return total;
	},
	allSelect: function () {
		var list = this.data.goodsList.list;
		var allSelect = false;
		for (var i = 0; i < list.length; i++) {
			var curItem = list[i];
			if (curItem.active) {
				allSelect = true;
			} else {
				allSelect = false;
				break;
			}
		}
		return allSelect;
	},
	noSelect: function () {
		var list = this.data.goodsList.list;
		var noSelect = 0;
		for (var i = 0; i < list.length; i++) {
			var curItem = list[i];
			if (!curItem.active) {
				noSelect++;
			}
		}
		if (noSelect == list.length) {
			return true;
		} else {
			return false;
		}
	},
	setGoodsList: function (saveHidden, total, allSelect, noSelect, list) {
		for (var i = 0; i < list.length; i++) {
			list[i].HCS_Firstcount = parseInt(list[i].HCS_Firstcount);
		}
		this.setData({
			goodsList: {
				saveHidden: saveHidden,
				totalPrice: total,
				allSelect: allSelect,
				noSelect: noSelect,
				list: list
			}
		});
		var shopCarInfo = {};
		var tempNumber = 0;
		shopCarInfo.shopList = list;
		for (var i = 0; i < list.length; i++) {
			tempNumber = tempNumber + list[i].HCS_Firstcount
		}
		shopCarInfo.shopNum = tempNumber;
		wx.setStorage({
			key: "shopCarInfo",
			data: shopCarInfo
		})
	},
	bindAllSelect: function () {
		var currentAllSelect = this.data.goodsList.allSelect;
		var list = this.data.goodsList.list;
		if (currentAllSelect) {
			for (var i = 0; i < list.length; i++) {
				var curItem = list[i];
				curItem.active = false;
			}
		} else {
			for (var i = 0; i < list.length; i++) {
				var curItem = list[i];
				curItem.active = true;
			}
		}

		this.setGoodsList(this.getSaveHide(), this.totalPrice(), !currentAllSelect, this.noSelect(), list);
	},

	jiaBtnTap: function (e) {
		var index = e.currentTarget.dataset.index;
		var list = this.data.goodsList.list;
		if (index !== "" && index != null) {
			if (list[parseInt(index)].C_Firstcount < 1000000) {
				list[parseInt(index)].C_Firstcount++;
				this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
			}
		}
	},

	jianBtnTap: function (e) {
		var index = e.currentTarget.dataset.index;
		var list = this.data.goodsList.list;
		if (index !== "" && index != null) {
			if (list[parseInt(index)].C_Firstcount > 1) {
				list[parseInt(index)].C_Firstcount--;
				this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
			}
		}
	},

	editTap: function () {
		var list = this.data.goodsList.list;
		for (var i = 0; i < list.length; i++) {
			var curItem = list[i];
			curItem.active = false;
		}
		this.setGoodsList(!this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
	},

	saveTap: function () {
		var list = this.data.goodsList.list;
		for (var i = 0; i < list.length; i++) {
			var curItem = list[i];
			curItem.active = true;
		}
		this.setGoodsList(!this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
	},

	getSaveHide: function () {
		var saveHidden = this.data.goodsList.saveHidden;
		return saveHidden;
	},

	deleteSelected1: function (e) {
		var _this = this;
		var list = _this.data.goodsList.list;
		var list_all = _this.data.goodsList.list;
		list = list.filter(function (curGoods) {
			return curGoods.active;
		});
		var firstcount = "";
		var delcs_id = "";
		var csid = "";
		for (let i = 0; i < list_all.length; i++) {
			csid += list_all[i].HCS_ID + ",";
			firstcount += list_all[i].HCS_Firstcount + ",";
		}
		for (let i = 0; i < list.length; i++) {
			delcs_id += list[i].HCS_ID + ",";
		}

		wx.request({
			url: Keys.data.url_Car,
			data: {
				DataType: 'Car_Del',
				csid: csid,
				delcs_id: delcs_id,
				firstcount: firstcount,
			},

			success: function (res) {
				if (res.data.isok == "true") {
					_this.onShow();
				}
			},
		});
	},
	deleteSelected: function () {
		var list = this.data.goodsList.list;
		list = list.filter(function (curGoods) {
			return !curGoods.active;
		});
		this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
	},

	toPayOrder1: function () {

		console.log(this.data.goodsList)
		var temp = true;
		for (var i = 0, j = this.data.goodsList.list.length; i < j; i++) {
			if (this.data.goodsList.list[i].active == true) {
				temp = false;
			}
		}
		if (temp == true)
			return;

		//组建立即购买信息
		var buyNowInfo = this.buliduBuyNowInfo();

		if (this.data.goodsList.list.length > 0) {
			wx.navigateTo({
				url: "/pages/Fl_ConfirmOrder/index?buyInfo=" + JSON.stringify(buyNowInfo)
			})
		}

	},

  /**
* 组建立即购买信息
*/
	buliduBuyNowInfo: function () {

		this.data.goodsList.list = this.data.goodsList.list.filter(function (curGoods) {
			return curGoods.active;
		});

		var buyNowInfo = {};
		if (!buyNowInfo.shopNum) {
			buyNowInfo.shopNum = 0;
		}
		if (!buyNowInfo.shopList) {
			buyNowInfo.shopList = [];
		}

		for (var i = 0; i < this.data.goodsList.list.length; i++) {
			var shopCarMap = {};
			shopCarMap.pic = this.data.goodsList.list[i].PI_Image;
			shopCarMap.name = this.data.goodsList.list[i].C_Name;
			shopCarMap.num = this.data.goodsList.list[i].C_Firstcount;
			shopCarMap.price = this.data.goodsList.list[i].P_Price;
			shopCarMap.c_id = this.data.goodsList.list[i].C_ID;
			shopCarMap.p_id = this.data.goodsList.list[i].P_ID;
			buyNowInfo.shopList.push(shopCarMap)
		}

    console.log("mmmmmmmm");
    console.log(this.data.goodsList.list);
		return buyNowInfo;
	},

	toPayOrder: function () {
		wx.showLoading();
		var that = this;
		if (this.data.goodsList.noSelect) {
			wx.hideLoading();
			return;
		}
		// 重新计算价格，判断库存
		var shopList = [];
		var shopCarInfoMem = wx.getStorageSync('shopCarInfo');
		if (shopCarInfoMem && shopCarInfoMem.shopList) {
			// shopList = shopCarInfoMem.shopList
			shopList = shopCarInfoMem.shopList.filter(entity => {
				return entity.active;
			});
		}
		if (shopList.length == 0) {
			wx.hideLoading();
			return;
		}
		var isFail = false;
		var doneNumber = 0;
		var needDoneNUmber = shopList.length;
		for (let i = 0; i < shopList.length; i++) {
			if (isFail) {
				wx.hideLoading();
				return;
			}
			let carShopBean = shopList[i];
		}
	},
	DeleteClicked: function (e) {
		var _this = this;
		var index = e.currentTarget.dataset.index;

		wx.showModal({
			title: '提示',
			content: '是否删除此商品？',
			success: function (res) {
				if (res.confirm) {
					wx.request({
						url: "https://www.chnode.xyz/api/Car.ashx?",
						data: {
							DataType: "CarDel",
							Token: getApp().data.Token,
							C_ID: _this.data.goodsList.list[index].C_ID

						},
						success: (res) => {
							if (res.data.isok == "fasle") {
							} else {
								_this.data.goodsList.list.splice(index, 1)
								_this.setData({
									goodsList: _this.data.goodsList
								})

							}
						},
						complete: (res) => {
							wx.hideLoading()
						}
					})
				}
			}
		})


	}




})
